//
//  JMPeripheralView.m
//  OpenSesame
//
//  Created by zhangjiaming on 16/1/6.
//  Copyright © 2016年 OpenSesame. All rights reserved.
//

#import "JMPeripheralView.h"
@interface JMPeripheralView ()

@property (copy, nonatomic) NSString *peripheralName;
@property (weak, nonatomic) UIImageView *scanView;
@end

@implementation JMPeripheralView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        UILabel *label1 = [[UILabel alloc] init];
        [self addSubview:label1];
        label1.frame = CGRectMake(10, 0, 100, 50);
        label1.text = @"已连接设备:";
        label1.textColor = JMColor(153, 153, 153);
        label1.font = [UIFont systemFontOfSize:12];
        
        UILabel *lastConnectLable = [[UILabel alloc] init];
        [self addSubview:lastConnectLable];
        lastConnectLable.frame = CGRectMake(label1.x + label1.width, label1.y, self.width - label1.x - label1.width, label1.height);
        lastConnectLable.textAlignment = NSTextAlignmentCenter;
        lastConnectLable.text = @"无已连接设备";
        self.lastConncetLabel = lastConnectLable;
        
        // 第二行
        UIView *view2 = [[UIView alloc] init];
        [self addSubview:view2];
        view2.frame = CGRectMake(0, label1.y + label1.height, self.width, 1);
        view2.backgroundColor = JMColor(242, 242, 242);
        
        UILabel *label2 = [[UILabel alloc] init];
        [self addSubview:label2];
        label2.frame = CGRectMake(label1.x, view2.y, label1.width, label1.height);
        label2.text = @"可连接设备:";
        label2.textColor = JMColor(153, 153, 153);
        label2.font = [UIFont systemFontOfSize:12];
        
        // 添加旋转动画
        UIImageView *scanView = [[UIImageView alloc] initWithFrame:CGRectMake(self.width - 35, label2.y + 15, 20, 20)];
        scanView.image = [UIImage imageNamed:@"rotatepic"];
        [self addSubview:scanView];
        self.scanView = scanView;
        [self startRotate];
        
        // 添加确定、取消按钮
        CGFloat confirmBtnW = (self.width - 20) * 0.5;
        CGFloat confirmBtnH = confirmBtnW * 0.5;
        CGFloat confirmBtnY = self.height - confirmBtnH;
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:cancelBtn];
        cancelBtn.frame = CGRectMake(0, confirmBtnY, confirmBtnW, confirmBtnH);
        [cancelBtn setImage:[UIImage imageNamed:@"cancelbtn0"] forState:UIControlStateNormal];
        [cancelBtn setImage:[UIImage imageNamed:@"cancelbtn1"] forState:UIControlStateHighlighted];
        [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:confirmBtn];
        confirmBtn.frame = CGRectMake(cancelBtn.width + 20, confirmBtnY, confirmBtnW, confirmBtnH);
        [confirmBtn setImage:[UIImage imageNamed:@"okbtn0"] forState:UIControlStateNormal];
        [confirmBtn setImage:[UIImage imageNamed:@"okbtn1"] forState:UIControlStateHighlighted];
        [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UITableView *tableView = [[UITableView alloc] init];
        self.tableView = tableView;
        CGFloat tableViewY = label2.y + label2.height;
        tableView.frame = CGRectMake(0, tableViewY, self.width, confirmBtn.y - tableViewY);
        [self addSubview:tableView];
        tableView.showsVerticalScrollIndicator = NO;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        // 去掉多余的cell分割线
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
        [tableView setTableFooterView:view];
        // 监听获取用户选择的外设名
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(obtain:) name:@"obtainPeripheralName" object:nil];
        self.layer.cornerRadius = 4.0;
    }
    return self;
}
#pragma mark1111111111111111111111111111111111111
/**
 *  旋转动画
 */
- (void)startRotate {
    [UIView animateWithDuration:0.1 animations:^{
        self.scanView.transform = CGAffineTransformRotate(self.scanView.transform, -30.0);
    } completion:^(BOOL finished) {
        [self startRotate];
    }];
}
// 监听获取用户选择的外设名
- (void)obtain:(NSNotification *)notificaton {
    
    self.peripheralName = notificaton.userInfo[@"periphralName"];
    
}

/**
 *  确定选择外设,通知代理确定选择外设
 */
- (void)confirmBtnClick:(UIButton *)confirmBnt {
    confirmBnt.titleLabel.text = self.peripheralName;
    if ([self.delegate respondsToSelector:@selector(peripheralView:didClickedConfirmBtn:)]) {
        [self.delegate peripheralView:self didClickedConfirmBtn:confirmBnt];
    }
}

/**
 *  通知代理删除蒙版
 */
- (void)cancelBtnClick:(UIButton *)confirmBnt {
    if ([self.delegate respondsToSelector:@selector(removeShieldView:)])
        [self.delegate removeShieldView:self];
}
@end
